package com.cn.cash.baselib;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.support.multidex.MultiDexApplication;

import java.util.Stack;


/**
 * Created by CN on 2016/10/31.
 */
public class CNApp extends MultiDexApplication {
    private static CNApp INSTANCE;
    private Stack<Activity> stackActivity;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        syncIsDebug(this);
        BaseManager.init(this, isDebug());
    }

    public static CNApp getInstance() {
        return INSTANCE;
    }

    private Boolean isDebug = null;

    public boolean isDebug() {
        return isDebug == null ? false : isDebug.booleanValue();
    }

    private void syncIsDebug(Context context) {
        if (isDebug == null) {
            isDebug = context.getApplicationInfo() != null &&
                    (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        }
    }

    public Stack<Activity> getStackActivity() {
        if(stackActivity == null){
            stackActivity = new Stack<>();
        }
        return stackActivity;
    }

    public Activity getTopActivity() {
        if (stackActivity != null && stackActivity.size() > 0) {
            return stackActivity.get(stackActivity.size() - 1);
        }
        return null;
    }

    public void addActivity(Activity appCompatActivity) {
        if (stackActivity == null)
            stackActivity = new Stack<>();
        if (appCompatActivity != null)
            stackActivity.add(appCompatActivity);
    }

    public void removeActivity(Activity appCompatActivity) {
        if (stackActivity != null && appCompatActivity != null) {
            stackActivity.remove(appCompatActivity);
        }
    }
    public void removeAllActivity() {
        if (stackActivity != null) {
            for (Activity activity : stackActivity) {
                activity.finish();
            }
            stackActivity.clear();
        }
    }

    public int getActivityCount() {
        if (stackActivity != null) {
            return stackActivity.size();
        }
        return 0;
    }
}
